package com.lz.comm.widget;

import android.text.InputFilter;
import android.text.Spanned;

/**
 * Created by you on 2019/9/27.
 * 特殊字符的长度输入限制,  如一个中文相当两个英文的长度
 */

public class SpecialLengthFilter implements InputFilter {

    private final int mMax;

    public SpecialLengthFilter(int max) {
        mMax = max;
    }

    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        CharSequence sourceWrap = new CharSequenceWrap(source);
        CharSequence destWrap = new CharSequenceWrap(dest);
        int keepWrap = mMax - (destWrap.length() - (dend - dstart));
        //Log.i("you", source + " start " + start + " end " + end + " dest " + dest + " dstart " + dstart + " dend " + dend + " keep " + keepWrap);
        if (keepWrap <= 0) {
            return "";
        } else if (keepWrap >= end - start + sourceWrap.length() - source.length()) {
            return null; // keep original
        } else {
            keepWrap += start;
            if (Character.isHighSurrogate(sourceWrap.charAt(keepWrap - 1))) {
                --keepWrap;
                if (keepWrap == start) {
                    return "";
                }
            }
            //Log.i("you", "sub " + start + " " + keepWrap);
            return sourceWrap.subSequence(start, keepWrap);
        }
    }

    /**
     * @return the maximum length enforced by this input filter
     */
    public int getMax() {
        return mMax;
    }

    /**
     * 包装模式
     */
    private static class CharSequenceWrap implements CharSequence {

        private final CharSequence sequence;

        private final int length;

        public CharSequenceWrap(CharSequence sequence) {
            this.sequence = sequence;
            length = length(this.sequence);
        }

        @Override
        public int length() {
            return length;
        }

        @Override
        public char charAt(int index) {
            return sequence.charAt(specialIndex(index));
        }

        private int specialIndex(int index) {
            int specialIndex = 0;
            for (int i = 0; i < sequence.length(); i++) {
                char c = sequence.charAt(i);
                specialIndex += specialCharLength(c);
                if (specialIndex > index){
                    return i;
                }
            }
            return index;
        }

        @Override
        public CharSequence subSequence(int start, int end) {
            int specialEnd = specialIndex(end);
            if (specialEnd < start) specialEnd = start;
            return sequence.subSequence(start, specialEnd);
        }

        /**
         * 是否为特殊字符,特殊字符占两个英文的位置
         *
         * @param c
         * @return
         */
        static int specialCharLength(char c) {
            try {
                int length = String.valueOf(c).getBytes("utf-8").length;
                return length > 1 ? 2 : length;
            } catch (Exception e) {
                return 1;
            }
        }

        static int length(CharSequence charSequence) {
            int length = 0;
            for (int i = 0; i < charSequence.length(); i++) {
                length += specialCharLength(charSequence.charAt(i));
            }
            return length;
        }
    }

}
